package com.csii.adhoc.controller.board;

import com.csii.adhoc.annotation.OperationLog;
import com.csii.adhoc.board.BoardDtlPo;
import com.csii.adhoc.board.BoardDtlVo;
import com.csii.adhoc.board.service.BoardDtlService;
import com.csii.adhoc.util.DateUtils;
import com.csii.result.APIResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @description: 仪表盘明细控制层
 * @author: panChuanJiang
 * @create: 2021-02-04 13:52
 */
@Slf4j
@RestController
@RequestMapping("boardDtl")
public class BoardDtlController {

    private BoardDtlService boardDtlService;

    @RequestMapping("savePanelInfo")
    @OperationLog("仪表盘组件关联维护")
    public APIResult<?> savePanelInfo(@RequestBody BoardDtlVo vo) {
        String userId = String.valueOf(SecurityUtils.getSubject().getPrincipal());
        vo.setUpdatedBy(userId)
                .setUpdatedDt(DateUtils.getDateString("yyyyMMddHHmmss"));
        String unitId = boardDtlService.boardDtlMaintain(vo, vo.getOptMode());
        HashMap<String, String> resMap = new HashMap<>();
        resMap.put("unitId", unitId);
        return APIResult.successResult(resMap);
    }

    @RequestMapping("queryPanelInfo")
    public APIResult<?> queryPanelInfo(@RequestBody(required = false) BoardDtlVo boardDtlPo) {
        List<BoardDtlVo> boardDtlVos = boardDtlService.queryPanelInfo(boardDtlPo);
        if (!Optional.ofNullable(boardDtlPo.getIsTableIdNull()).orElse(true)) {
            boardDtlVos = boardDtlVos.stream().filter(x -> !StringUtils.isEmpty(x.getTableId())).collect(Collectors.toList());
        }
        return APIResult.successResult(boardDtlVos);
    }

    @RequestMapping("saveListPanelInfo")
    @OperationLog("批量保存仪表盘组件信息")
    public APIResult<?> saveListPanelInfo(@RequestBody List<BoardDtlPo> list) {
        String userId = Optional.ofNullable(String.valueOf(SecurityUtils.getSubject().getPrincipal())).orElse("未知用户");
        list.forEach(x -> x.setUpdatedBy(userId)
                .setUpdatedDt(DateUtils.getDateString("yyyyMMddHHmmss")));
        boardDtlService.saveListPanelInfo(list);
        return APIResult.successResult(null);
    }

    @Autowired
    public void setBoardDtlService(BoardDtlService boardDtlService) {
        this.boardDtlService = boardDtlService;
    }
}
